function formatParams(params) {
    let paramsArr = [];
    for (let key in params) {
        let item = `${key}=${params[key]}`;
        paramsArr.push(item)
    }
    return paramsArr.length ? `${paramsArr.join("&")}` : '';
}
function ajax({ url, method = "GET", params }) {
    return new Promise((resolve, reject) => {
        let xml = new XMLHttpRequest();
        xml.addEventListener("readystatechange", () => {
            if (xml.readyState !== 4) {
                return
            }
            if (xml.status == 200) {
                // 数据
                // xml.response;
                resolve(JSON.parse(xml.response))
            } else {
                reject()
            }
        })
        // 处理 get post 参数
        if (method == "GET" || method == "get") {
            let getParams = formatParams(params);
            url += getParams ? '?' + getParams : '';
            xml.open(method, url);
            xml.send();
        } else {
            xml.open(method, url);
            xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xml.send(formatParams(params));
        }
    })
};